package com.wen.cim.route.exception;

import com.wen.cim.common.enums.StatusEnum;
import com.wen.cim.common.exception.CIMException;
import com.wen.cim.common.res.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 文洪武
 * @title: ExceptionHandlingController
 * @projectName cim-master
 * @description: 异常处理类
 * @date 2021/11/2514:20
 */
@ControllerAdvice
public class ExceptionHandlingController {

    private static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlingController.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody()
    public BaseResponse handlerExceptions(Exception ex) {
        LOGGER.error("exception", ex);
        BaseResponse response = new BaseResponse();
        response.setCode(StatusEnum.SYSTEM_FAIL.getCode());
        response.setMessage(StatusEnum.SYSTEM_FAIL.getMessage());
        return response;
    }

    @ExceptionHandler(CIMException.class)
    @ResponseBody()
    public BaseResponse handlerCIMExceptions(CIMException ex) {
        LOGGER.error("exception", ex);
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(ex.getErrorCode());
        baseResponse.setMessage(ex.getMessage());
        return baseResponse;
    }
}
